<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('药品信息列表')"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="col-sm-12 search-collapse">
            <form id="formId" class="layui-form">
                <div class="layui-form-item">
                    <label class="layui-form-label">名称：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入名称" class="layui-input"/>
                    </div>
                    <label class="layui-form-label">供应商：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="hospitalSupplier" placeholder="请输入供应商" class="layui-input"/>
                    </div>
                    <label class="layui-form-label">药品编码：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="number" placeholder="请输入药品编码" class="layui-input"/>
                    </div>
                    <label class="layui-form-label">保质期：</label>
                    <div class="layui-input-inline">
                        <input type="text" name="warrenty" placeholder="请输入保质期（月）" class="layui-input"/>
                    </div>
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="druginfo-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="druginfo-table" lay-filter="druginfo-table"></table>
        <script type="text/html" id="druginfo-toolbar">
            <button sec:authorize="hasPermission('/hospital/druginfo/add','hospital:druginfo:add')"
                    class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/hospital/druginfo/remove','hospital:druginfo:remove')"
                    class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
        <script type="text/html" id="druginfo-bar">
            <button sec:authorize="hasPermission('/hospital/druginfo/edit','hospital:druginfo:edit')"
                    class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>
            </button>
            <button sec:authorize="hasPermission('/hospital/druginfo/remove','hospital:druginfo:remove')"
                    class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                    class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script>
    layui.use(['table', 'form', 'jquery', 'dictionary'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;

        let prefix = "/hospital/druginfo/";

        let cols = [
            [
                {type: 'checkbox'},
                {
                    field: 'id',
                    title: '主键'
                },
                {
                    field: 'name',
                    title: '名称'
                },
                {
                    field: 'hospitalSupplier',
                    title: '供应商',
                    templet: supplierName
                },
                {
                    field: 'producttime',
                    title: '生产时间'
                },
                {
                    field: 'warrenty',
                    title: '保质期（月）'
                },
                {
                    field: 'number',
                    title: '药品编码'
                },
                {
                    field: 'price',
                    title: '价格'
                },
                {
                    field: 'stock',
                    title: '库存'
                },
                {title: '操作', toolbar: '#druginfo-bar', align: 'center', width: 130}
            ]
        ]

        table.render({
            elem: '#druginfo-table',
            url: prefix + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#druginfo-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(druginfo-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(druginfo-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(druginfo-query)', function (data) {
            table.reload('druginfo-table', {where: data.field})
            return false;
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增药品信息',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改药品信息',
                shade: 0.1,
                area: ['550px', '500px'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该药品信息', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些药品信息', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('druginfo-table');
                            });
                        } else {
                            layer.msg(result.msg, {icon: 2, time: 1000});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('druginfo-table', {where: param});
        }

        function supplierName(d) {
            if (d.hospitalSupplier.length <= 0) {
                return "";
            }
            $.ajax({
                url: '/hospital/supplier/getSupplierById',
                data: {id: d.hospitalSupplier},
                async: false,
                success: function (result) {
                    if (result.success) {
                        return result.data.name;
                    }
                },
                error: function (result) {
                    return "";
                }
            });
        }
    })
</script>
</body>
</html>